package com.aaa.project.controller;


import com.aaa.entity.UUserInfo;
import com.aaa.project.service.IUUserInfoService;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangxuhui
 * @since 2022-12-30
 */
@RestController
@RequestMapping("/u-user-info")
public class UUserInfoController {

    @Resource
    private IUUserInfoService userInfoService;
    //根据用户名获取用户信息
    @PostMapping("getUserByName/{username}")
    public UUserInfo getUser(@PathVariable String username){
        QueryWrapper<UUserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uname",username);
        System.out.println(queryWrapper);
        return userInfoService.getOne(queryWrapper);
    }
    //根据用户手机号获取信息
    @PostMapping("getUserByPhone/{telephone}")
    public UUserInfo getUserPhone(@PathVariable String telephone){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",telephone);
        return userInfoService.getOne(queryWrapper);
    }
    //用户注册（添加）
    @PostMapping("/insertAddr")
    public int insertAddr( String telephone, String repassword){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",telephone);
        int telephoneCount = userInfoService.count(queryWrapper);
        if(telephoneCount!=0){
            return 400;
        }else {
            UUserInfo uUserInfo=new UUserInfo();
            uUserInfo.setTelephone(telephone);
            uUserInfo.setPassword(repassword);
            uUserInfo.setCreatetime(String.valueOf(LocalDateTime.now()));
            uUserInfo.setStatus(0);
            BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
            uUserInfo.setPassword(bCryptPasswordEncoder.encode(repassword));
            uUserInfo.setSalt("123");
            userInfoService.save(uUserInfo);
            return 200;
        }
    }


    //查询登录用户信息
    @GetMapping("findAll")
    public Result<UUserInfo> findAll(@RequestParam("user_name") String name){
        System.out.println("从前端拿到的商家用户名是"+name);
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",name);
        System.out.println("准备查找数据");
        UUserInfo uUserInfo = userInfoService.getOne(queryWrapper);
        return  new Result(uUserInfo);
    }
    //修改
    @PostMapping("updateMessage")
    public Result updateMessage(@RequestBody UUserInfo uUserInfo){
        uUserInfo.setUpdatetime(String.valueOf(LocalDateTime.now()));
        uUserInfo.setUpdateman(uUserInfo.getUname());
        return new Result<>(userInfoService.updateById(uUserInfo));
    }
    /**
     * 修改密码
     */


}

